package com.gjj.abox.account;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gjj.abox.BaseActivity;
import com.gjj.abox.constant.Constants;
import com.gjj.abox.custom.MessageDialog;
import com.gjj.abox.database.R;
import com.gjj.abox.entity.Account;
import com.gjj.abox.utils.AnimalUtils;
import com.gjj.abox.utils.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddAccountActivity extends BaseActivity {

    @Bind(R.id.iv_insert_activity_back)
    ImageView ivInsertActivityBack;
    @Bind(R.id.iv_insert_activity_add)
    ImageView ivInsertActivityAdd;
    @Bind(R.id.et_add_account_name)
    EditText etAddAccountName;
    @Bind(R.id.et_add_account_id)
    EditText etAddAccountId;
    @Bind(R.id.et_add_account_pwd)
    EditText etAddAccountPwd;
    @Bind(R.id.et_add_account_phone)
    EditText etAddAccountPhone;
    @Bind(R.id.et_add_account_email)
    EditText etAddAccountEmail;
    @Bind(R.id.et_add_account_question)
    EditText etAddAccountQuestion;
    @Bind(R.id.btn_address_edit_delete)
    Button btnAddressEditDelete;
    @Bind(R.id.rl_address_edit_delete)
    RelativeLayout rlAddressEditDelete;
    @Bind(R.id.et_add_account_mark)
    EditText etAddAccountMark;
    @Bind(R.id.rl_insert_activity_add)
    RelativeLayout rlInsertActivityAdd;
    @Bind(R.id.tv_add_account_title)
    TextView tvAddAccountTitle;
    @Bind(R.id.ll_account_id)
    LinearLayout llAccountId;


    private String u_name, platformName, accountName;
    private String type;
    private boolean isEditPage, isEditing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_account);
        ButterKnife.bind(this);
        getParas();
    }

    private void getParas() {
        isEditPage = false;
        isEditing = false;
        accountName = "";
        type = "";
        type = getIntent().getStringExtra(Constants.Edit_Page_Type);
        u_name = getIntent().getStringExtra(Constants.UserName);
        platformName = getIntent().getStringExtra(Constants.Platform_Name);
        accountName = getIntent().getStringExtra(Constants.AccountId);
        if (type.equals(Constants.Is_Add)) {
            setEdit();
        } else if (type.equals(Constants.Is_Detail)) {
            isEditPage = true;
            setData();
        }
    }

    @OnClick({R.id.iv_insert_activity_back, R.id.rl_insert_activity_add, R.id.btn_address_edit_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_insert_activity_back:
                back();
                break;
            case R.id.rl_insert_activity_add:
                if (type.equals(Constants.Is_Detail)) {
                    if (!isEditing) {
                        setEdit();
                    } else {
                        modifyData();
                    }
                } else {
                    saveData();
                }
                break;
            case R.id.btn_address_edit_delete:
                delete();
                break;
        }
    }

    //保存数据
    private void saveData() {
        if (!checkInput()) {
            return;
        }
        if (boxDb.checkAccountNull(platformName, u_name, etAddAccountId.getText().toString().trim())) {
            ToastUtil.showShortToast(AddAccountActivity.this, "该账户已有记录！");
            AnimalUtils.shakeAnimal(etAddAccountId);
        } else {
            boxDb.insertAccount(u_name,
                    platformName,
                    etAddAccountId.getText().toString().trim(),
                    etAddAccountPwd.getText().toString().trim(),
                    etAddAccountName.getText().toString().trim(),
                    etAddAccountPhone.getText().toString().trim(),
                    etAddAccountEmail.getText().toString().trim(),
                    etAddAccountQuestion.getText().toString().trim(),
                    etAddAccountMark.getText().toString().trim());
            type = Constants.Is_Detail;
            accountName = etAddAccountId.getText().toString().trim();
            setData();
        }
    }

    //编辑数据
    private void modifyData() {
        if (!checkInput()) {
            return;
        }
        isEditing = false;
        if (!accountName.equals(etAddAccountId.getText().toString().trim()) && boxDb.checkAccountNull(platformName, u_name, etAddAccountId.getText().toString().trim())) {
            ToastUtil.showShortToast(AddAccountActivity.this, "该账户已有记录！");
            AnimalUtils.shakeAnimal(etAddAccountId);
            return;
        }
        boxDb.updataAccount(etAddAccountId.getText().toString().trim(),
                etAddAccountName.getText().toString().trim(),
                etAddAccountPwd.getText().toString().trim(),
                etAddAccountPhone.getText().toString().trim(),
                etAddAccountEmail.getText().toString().trim(),
                etAddAccountQuestion.getText().toString().trim(),
                etAddAccountMark.getText().toString().trim(),
                u_name, platformName, accountName);
        accountName = etAddAccountId.getText().toString().trim();
        setData();
    }


    private void setEdit() {
        isEditing = true;
        ivInsertActivityAdd.setBackgroundResource(R.mipmap.complete);
        llAccountId.setVisibility(View.VISIBLE);
        rlAddressEditDelete.setVisibility(View.GONE);
        tvAddAccountTitle.setText("编辑");
        setFocusStatus(true);
    }

    private void setData() {
        ivInsertActivityAdd.setBackgroundResource(R.drawable.edit_selector);
        rlAddressEditDelete.setVisibility(View.VISIBLE);
        setFocusStatus(false);
        Account account = boxDb.queryAccountAll(u_name, platformName, accountName);
        etAddAccountName.setText(getValidData(account.getAccountNickName()));
        llAccountId.setVisibility(View.GONE);
        etAddAccountId.setText(accountName);
        etAddAccountPwd.setText(getValidData(account.getAccountPwd()));
        etAddAccountPhone.setText(getValidData(account.getAccountTel()));
        etAddAccountEmail.setText(getValidData(account.getAccountEmail()));
        etAddAccountQuestion.setText(getValidData(account.getAccountQuestion()));
        etAddAccountMark.setText(getValidData(account.getAccountMark()));
        tvAddAccountTitle.setText(account.getAccountName());
        closeInputKey();
    }


    private void closeInputKey() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private String getValidData(String string) {
        if (string == null) {
            return "";
        }
        return string;
    }

    private void setFocusStatus(boolean status) {
        //昵称不可点击
        etAddAccountName.setFocusable(status);
        etAddAccountName.setFocusableInTouchMode(status);
        //id不可点击
        etAddAccountId.setFocusable(status);
        etAddAccountId.setFocusableInTouchMode(status);
        //密码不可点击
        etAddAccountPwd.setFocusable(status);
        etAddAccountPwd.setFocusableInTouchMode(status);
        //手机不可点击
        etAddAccountPhone.setFocusable(status);
        etAddAccountPhone.setFocusableInTouchMode(status);
        //邮箱不可点击
        etAddAccountEmail.setFocusable(status);
        etAddAccountEmail.setFocusableInTouchMode(status);
        //答案不可点击
        etAddAccountQuestion.setFocusable(status);
        etAddAccountQuestion.setFocusableInTouchMode(status);
        //备注不可点击
        etAddAccountMark.setFocusable(status);
        etAddAccountMark.setFocusableInTouchMode(status);
    }

    //检查输入
    private boolean checkInput() {
        if (etAddAccountName.getText().toString().trim().equals("")) {
            AnimalUtils.shakeAnimal(etAddAccountName);
            ToastUtil.showShortToast(AddAccountActivity.this, "请输入昵称！");
            return false;
        } else if (etAddAccountId.getText().toString().trim().equals("")) {
            AnimalUtils.shakeAnimal(etAddAccountId);
            ToastUtil.showShortToast(AddAccountActivity.this, "请输入该平台登录的ID！");
            return false;
        } else if (etAddAccountPwd.getText().toString().trim().equals("")) {
            AnimalUtils.shakeAnimal(etAddAccountPwd);
            ToastUtil.showShortToast(AddAccountActivity.this, "请输入该平台登录的密码！");
            return false;
        }
//        else if (etAddAccountPhone.getText().toString().trim().equals("")) {
//            AnimalUtils.shakeAnimal(etAddAccountPhone);
//            ToastUtil.showShortToast(AddAccountActivity.this, "请输入该平台绑定的手机号！");
//            return false;
//        }
        return true;
    }

    //返回
    private void back() {
        MessageDialog.showConfirmDialog(AddAccountActivity.this, "您要放弃此次编辑吗？", new MessageDialog.DialogConfirmClickListener() {
            @Override
            public void confirm() {
                finish();
            }
        });
    }

    //删除
    private void delete() {
        MessageDialog.showConfirmDialog(AddAccountActivity.this, "您确定要删除该条信息吗？", new MessageDialog.DialogConfirmClickListener() {
            @Override
            public void confirm() {
                boxDb.deleteAccount(u_name, platformName, accountName);
                finish();
            }
        });
    }
}
